package com.itheima.handlerException;

import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.exception.BusinessRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.servlet.ServletException;

@RestControllerAdvice
//代码出现异常，通知执行
//@RestControllerAdvice=@ControllerAdvice+@ResponseBody
@Slf4j
public class MyHandlerException {
    /*@ExcetionHandler:要对那些异常进行拦截
    * @ResponseStatus:响应状态的配置
    * @param e
    * @return
    */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(BusinessRuntimeException.class)
    public Result handlerRuntimeException(BusinessRuntimeException e){
        log.debug("error:"+e);
        return new Result(false,e.getMessage());
    }
    @ExceptionHandler({
            ServletException.class,
            HttpMessageConversionException.class,
            MethodArgumentNotValidException.class,
            MethodArgumentTypeMismatchException.class
    })
    public Result handlerOtherException(Exception e) throws Exception{
        log.debug("出错了");
        throw e;
    }
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({
            Exception.class
    })
    public Result handlerException(Exception e)throws Exception{
        log.debug("error:"+e);
        return new Result(false, MessageConst.ACTION_FAL);
    }
}
